<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/logo.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click='logout'>退出</el-button>
        </el-header>
        <!-- 主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区 -->
                <el-menu background-color="rgb(52, 68, 92)" text-color="#fff" active-text-color="#ffd04b" router unique-opened :collapse="isCollapse" :default-active="activePath">
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                        <!-- 一级菜单的模板区 -->
                        <template slot="title">
                          <!-- 图标 -->
                          <i :class="iconsObj[item.id]"></i>
                          <!-- 标题 -->
                          <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      // 默认不折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-form',
        101: 'iconfont icon-shangpu2',
        102: 'iconfont icon-shouye',
        145: 'iconfont icon-dt_icon_19'
      }
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单list
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res)
      this.menulist = res.data
    },
    // 点击按钮折叠菜单栏
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存路由链接状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
    height: 100%
}
.el-header{
  background-color: rgb(87, 189, 136);
  color: #fff;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  font-size: 20px;
  > div{
    display: flex;
    align-items: center;
    img{
        width: 50px;
    }
    span{
        margin-left: 15px;
    }
  }
}
.el-menu{
    border-right: none;
    > template{
        padding: 0px;
    }
}
.el-aside{
    background-color: rgb(52, 68, 92);
    color: #fff;
}
.el-main{
    background-color: rgb(240, 240, 240);
    color: black;
}
.toggle-button{
    background-color: rgb(66, 66, 66);
    font-size: 14px;
    line-height: 24px;
    text-align: center;
    color: #fff;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>
